<?php 
class Country_AdminCountryFieldController extends Core_Controller_Action_Admin{
    
    public function indexAction(){
        $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('country_admin_main', array(), '');
                
        $country_id = $this->_getParam('id', 0);
        
        if(!empty($country_id)){
            
            $country = Engine_Api::_()->getItem('country_country', $country_id);
            $this->view->country = $country;
        }
        
        $this->view->country_id = $country_id;
        
        $country_field_Tb = Engine_Api::_()->getDbTable('countryfields','country') ;
        
        $select = $country_field_Tb->select()
                                    ->where('country_id = ?', $country_id);
        
        $this->view->paginator = $paginator = Zend_Paginator::factory($select);
        $page = $this->_getParam('page',1);
        $paginator->setItemCountPerPage(10);
        $this->view->paginator = $paginator->setCurrentPageNumber($page);
        
    }
    
    public function addAction(){
        $request = $this->getRequest();
        $country_id = $this->_getParam('c_id', 0);
      	$this->view->form = $form = new Country_Form_Admin_Countryfield_Create();
        $form->setTitle('Create New Field');
        
        
        if($request->isPost()){
            
            if(!$form->isValid($request->getPost())) return;
            try{
                $values = $form->getValues();
            
                $country_field_Tb = Engine_Api::_()->getDbTable('countryfields','country') ;
                
                $new_item = $country_field_Tb->createRow();
                
                $new_item->field_id = $values['field_id'];
                $new_item->country_id = $country_id;
                $new_item->detail = $values['detail'];
                $new_item->creation_date = date("Y-m-d H:i:s");
                $new_item->modified_date = date("Y-m-d H:i:s");
                
                $new_item->save();
                
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Successfully.'))
            	));
            }
            catch(Exception $e){
                ///
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Error.'))
            	));
            }
            
            
        }
    }
    public function deleteAction()
    {
        
        $request = $this->getRequest();
        $country_field_id = $request->getParam('cf_id',0);
        $country_field = Engine_Api::_()->getItem('country_countryfield', $country_field_id);
        $this->view->form = $form = new Country_Form_Admin_Countryfield_Delete();
        if($request->isPost())
        {
            if(!$form->isValid($request->getPost())) return;
            $country_field->delete();
            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Successful.'))
            	));
            
        }
    }
    public function editAction(){
        $request = $this->getRequest();
        $country_field_id = $request->getParam('cf_id',0);
        $country_field = Engine_Api::_()->getItem('country_countryfield', $country_field_id);
      	
        $values = array();
        $values['field_id'] = $country_field->field_id;
        $values['detail'] = $country_field->detail;
          
        $this->view->form = $form = new Country_Form_Admin_Countryfield_Create();
        $form->setTitle('Edit Field');
        $form->populate($values);
        
        
        if($request->isPost()){
            
            if(!$form->isValid($request->getPost())) return;
            try{
                $values = $form->getValues();
                
                $country_field->field_id = $values['field_id'];
                $country_field->detail = $values['detail'];
                $country_field->modified_date = date("Y-m-d H:i:s");
                
                $country_field->save();
                
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 10,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Successfully.'))
            	));
            }
            catch(Exception $e){
                ///
                $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => 1000,
                    'parentRefresh'=> 10,
                    'messages' => array($this->view->translate('Adding Error.'))
            	));
            }
            
            
        }
    }
}
?>